<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="from" label-width="120px" style="margin-left: 120px; margin-top:30px" :rules="rules" :model="userInfo">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input v-model="userInfo.newPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="update">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="个人详情">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link :to="`/employees/print/${id}`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <UserInfo />
          </el-tab-pane>

          <el-tab-pane label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from './components/UserInfo.vue'
import JobInfo from './components/JobInfo.vue'
import { getuserallinfoapi } from '@/api/user'
import { saveUserDetailByIdRes } from '@/api/employees'
export default {
  name: 'Detail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userInfo: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空' }
        ],
        newPassword: [
          { required: true, message: '密码不能为空', trigger: ['blur'] },
          { min: 5, max: 9, message: '密码长度5-9位', trigger: ['change', 'blur'] }
        ]
      }

    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    // 回显请求
    async getUserDetailById() {
      const res = await getuserallinfoapi(this.id)
      console.log(res)
      this.userInfo.username = res.username
      this.userInfo.newPassword = res.password
    },

    // 更改账号请求
    async update() {
      try {
        await this.$refs.from.validate()
      } catch {
        return
      }
      await saveUserDetailByIdRes({
        id: this.id,
        username: this.userInfo.username,
        password: this.userInfo.newPassword
      })
      this.$message.success('更改成功')
    }
  }
}
</script>

<style lang="scss" scoped>
  .tooltip-box {
  position: absolute;
  right: 0;
  top: 0px;
  z-index: 999;
  }
</style>
